package cn.lysj.framework.api.controller;

import cn.lysj.framework.api.base.ApiContent;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.http.HttpUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.customer.domain.Customer;
import cn.lysj.framework.customer.service.CustomerService;
import cn.lysj.framework.customer.util.AliPayAuzLoginUtil;
import cn.lysj.framework.customer.util.WxAuzLoginUtil;
import cn.lysj.framework.store.domain.Store;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @ Author ：drj.
 * @ Date  ：Created in 14:47 2018/11/5
 * @ Description:
 **/
@RestController
@RequestMapping("/api/v1/user")
@Slf4j
public class AuzLoginController extends ApiContent {

    @Resource
    protected CustomerService customerService;

    private String redictUrl = "https://diet.lingyundata.com/static/ordering/"; //state1代表微信授权跳转首页

    private String redictCardUrl = "https://diet.lingyundata.com/static/ordering/getcoupon";//state2代表分享卡券




    @GetMapping(value = "/wx_login")
    public void wxLogin() throws IOException {
        StringBuilder sb = null;
        response.setCharacterEncoding("UTF-8");
        String state = request.getParameter("state"); // 获取要跳转的页面
        String code = request.getParameter("code");
        System.out.println("用户授权码:" + code + "跳转标识:" + state);
        if (state.equals("index")) {
            sb = new StringBuilder(redictUrl);
        }
        if (state.equals("coupon")) {
            sb = new StringBuilder(redictCardUrl);
        }
        Customer userApply = WxAuzLoginUtil.getUserInfo(request); //通过微信接口返回的User

        if (ParamUtil.isBlank(userApply.getFlagId())) {
            return;
        }
        Customer userQuery = customerService.getRepository().findByFlagId(userApply.getFlagId()); //通过数据库查询返回的User
        if (userQuery == null) { // 数据库还没有数据,新建一个用户
            System.out.println("对象为空");
            userQuery = customerService.getRepository().saveAndFlush(userApply);
        }
        sb.append("?customerId=" + userQuery.getId());
        response.sendRedirect(sb.toString());
    }

    @GetMapping(value = "/alipay_login")
    public void aliLogin() throws IOException {
        StringBuilder sb = null;
        response.setCharacterEncoding("UTF-8");
        String state = request.getParameter("state"); // 获取要跳转的页面
        String code = request.getParameter("auth_code");
        log.info("用户授权码:" + code + "跳转标识:" + state);
        if (state.equals("index")) {
            sb = new StringBuilder(redictUrl);
        }
        if (state.equals("coupon")) {
            sb = new StringBuilder(redictCardUrl);
        }
        Customer aliPayCustomer = AliPayAuzLoginUtil.getUserInfo(code);
        if (ParamUtil.isBlank(aliPayCustomer.getFlagId())) {
            return ;
        }
        Customer aliPayCustomerQuery = customerService.getRepository().findByFlagId(aliPayCustomer.getFlagId()); //通过数据库查询返回的User
        if (aliPayCustomerQuery == null) { // 数据库还没有数据,新建一个用户
            aliPayCustomerQuery=  customerService.getRepository().save(aliPayCustomer);
        }
        sb.append("?customerId=" + aliPayCustomerQuery.getId());
        response.sendRedirect(sb.toString());
    }

    @GetMapping("/get_customer_info")
    public Resp getCustomerInfo(String customerId) {
        if (ParamUtil.isBlank(customerId)) {
            return new Resp(Resp.NO_LOGIN, "参数错误");
        }
        Customer customer = customerService.findOne(customerId);
        if (customer == null) {
            return new Resp(Resp.NO_LOGIN, "用户信息有误!");
        }
        return new Resp(customer);
    }

    @GetMapping("get_flag")
    public Resp getFlag(String storeId) {
        Map<String, Object> map = new HashMap<>();
        map.put("flag", 0);
        //获取商户配置信息标识
        if (ParamUtil.isBlank(storeId)) {
            map.put("flag", 0);
        }
        Store store = storeRepository.findOne(storeId);
        if (null == store) {
            map.put("flag", 0);
        }
        //如果选择的是共同支持
        if (store.getSupporyPayWay().equals(3)) {
            map.put("flag", 3);
        }
        User user = userService.getRepository().findByStoreId(storeId);
        if (null == user) {
            map.put("flag", 0);
        }
        //阿里跟支付宝配置都未设置
        if (ParamUtil.isBlank(user.getAliAppId()) && ParamUtil.isBlank(user.getMchId())) {
            map.put("flag", 0);
        }
        if (!ParamUtil.isBlank(user.getAliAppId()) && ParamUtil.isBlank(user.getMchId())) {
            map.put("flag", 1);
        }
        if (ParamUtil.isBlank(user.getAliAppId()) && !ParamUtil.isBlank(user.getMchId())) {
            map.put("flag", 2);
        }
        if (!ParamUtil.isBlank(user.getAliAppId()) && !ParamUtil.isBlank(user.getMchId())) {
            map.put("flag", 3);
        }
        //商店是否营业
        Boolean isDoBusiness;

        if (ParamUtil.isBlank(store.getIsDoBusiness())) {
            isDoBusiness = true;
        } else {
            isDoBusiness = store.getIsDoBusiness();
        }
        if (store.getBusinessType() == 1) {
            String time[] = store.getBusinessTime().split("-");
            if (time.length == 2) {
                isDoBusiness = storeService.isDoBusiness(time);
                ;
            }
        }
        map.put("isDoBusiness", isDoBusiness);
        return new Resp(map);
    }
}
